package ServerToCOImpl;

import java.io.*;
import java.sql.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class ParseToXML {
	public static File parseToClassXML(ResultSet rs,String fileName){
		try {
			
		/**
		 * change the ResultSet
		 */
			ResultSetMetaData rsmd=rs.getMetaData();
			int count=rsmd.getColumnCount();
			String [] columnName=new String[count];
			
			for(int i=0;i<count;i++){
				columnName[i]=rsmd.getColumnName(i+1);
			}
			
			Document doc = (Document) DocumentHelper.createDocument();
		//	Element root=doc.addElement("classA");
			
			while(rs.next()){
				Element emp=doc.createElement("classA");
				for(int i=0;i<count;i++){
					Element column=((Document) emp).createElement(columnName[i]);
					if(rs.getObject(i+1)!=null)
						column.setTextContent(rs.getObject(i+1)+"");
					else
						column.setTextContent("");
				}
			}
	
			Writer w=new FileWriter("D:\\"+fileName+".xml");
			OutputFormat out=OutputFormat.createPrettyPrint();
			out.setEncoding("GB2312");
			XMLWriter xw=new XMLWriter(w,out);
			xw.write(doc);
			xw.close();
			w.close();
			
			return new File("D:\\"+fileName+".xml");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static File parseToStuXML(ResultSet rs,String fileName){
		try {
			
		/**
		 * change the ResultSet
		 */
			ResultSetMetaData rsmd=rs.getMetaData();
			int count=rsmd.getColumnCount();
			String [] columnName=new String[count];
			
			for(int i=0;i<count;i++){
				columnName[i]=rsmd.getColumnName(i+1);
			}
			 DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();  
			DocumentBuilder build = dFact.newDocumentBuilder();  
			Document doc = build.newDocument();  
		//	Element root=doc.createElement("Students");
			
			while(rs.next()){
				Element emp= doc.createElement("studentA");
				for(int i=0;i<count;i++){
					Element column=((Document) emp).createElement(columnName[i]);
					if(rs.getObject(i+1)!=null)
						column.setTextContent(rs.getObject(i+1)+"");
					else
						column.setTextContent("");
				}
			}
	
			Writer w=new FileWriter("D:\\"+fileName+".xml");
			OutputFormat out=OutputFormat.createPrettyPrint();
			out.setEncoding("GB2312");
			XMLWriter xw=new XMLWriter(w,out);
			xw.write(doc);
			xw.close();
			w.close();
			
			return new File("D:\\"+fileName+".xml");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static File parseToClassChoiceXML(ResultSet rs,String fileName){
		try {
			
		/**
		 * change the ResultSet
		 */
			ResultSetMetaData rsmd=rs.getMetaData();
			int count=rsmd.getColumnCount();
			String [] columnName=new String[count];
			
			for(int i=0;i<count;i++){
				columnName[i]=rsmd.getColumnName(i+1);
			}
			
			Document doc = (Document) DocumentHelper.createDocument();
			//Element root=doc.createElement("ClassChoices");
			
			while(rs.next()){
				Element emp=doc.createElement("choiceA");
				for(int i=0;i<count;i++){
					Element column=((Document) emp).createElement(columnName[i]);
					if(rs.getObject(i+1)!=null)
						column.setTextContent(rs.getObject(i+1)+"");
					else
						column.setTextContent("");
				}
			}
	
			Writer w=new FileWriter("D:\\"+fileName+".xml");
			OutputFormat out=OutputFormat.createPrettyPrint();
			out.setEncoding("GB2312");
			XMLWriter xw=new XMLWriter(w,out);
			xw.write(doc);
			xw.close();
			w.close();
			
			return new File("D:\\"+fileName+".xml");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
}
